﻿using System;

using MosCraft.Interfaces;
using MosCraft.Utilities;

namespace MosCraft.Terrain
{
    public class MapGenBase
    {
        internal int range;
        internal FastRandom rand;

        /// <summary>
        /// Class constructor
        /// </summary>
        public MapGenBase()
        {
            range = 8;
            rand = new FastRandom();
        }

        /// <summary>
        /// Returns the completed terrain stored in bytes
        /// </summary>
        /// <param name="world">active world</param>
        /// <param name="x">The X co-ord of the chunk</param>
        /// <param name="z">The Z co-ord of the chunk</param>
        /// <param name="blocks">Blocks in array of bytes</param>
        public void generate(IChunkGenerator ichunkprovider, World world, int x, int z, ref byte[] blocks)
        {
            int range = this.range;

            rand.setSeed(world.getSeed());
            long l = (rand.nextLong() / 2L) * 2L + 1L;
            long l1 = (rand.nextLong() / 2L) * 2L + 1L;

            for (int rangeX = x - range; rangeX <= x + range; rangeX++)
            {
                for (int rangeZ = z - range; rangeZ <= z + range; rangeZ++)
                {
                    rand.setSeed((long)rangeX * l + (long)rangeZ * l1 ^ world.getSeed());
                    recursiveGenerate(world, rangeX, rangeZ, x, z, ref blocks);
                }

            }
        }

        /// <summary>
        /// Protect internal through MapGenBase
        /// </summary>
        /// <param name="world">Active World</param>
        /// <param name="xRange">X range to generate</param>
        /// <param name="zRange">Z range to generate</param>
        /// <param name="x">The X co-ord of the chunk</param>
        /// <param name="z">The Z co-ord of the chunk</param>
        /// <param name="blocks">Blocks in array of byte</param>
        /// <returns>Modifed blocks in array of bytes</returns>
        virtual protected void recursiveGenerate(World world, int xRange, int zRange, int x, int z, ref byte[] blocks)
        {
        }
    }
}
